/**
 * 安装应用
 */
import { DependencyContainer, toSuccess, useTransaction } from '@sker/core'
import { SysAddon, SysAddonMenu } from '@sker/entities'
import { useReqBody } from '@sker/runtime'
import z from 'zod'
export default async (injector: DependencyContainer) => {
    const input = z.object({
        addon: z.string()
    })
    const body = input.parse(useReqBody())
    await useTransaction(async m => {
        const list = await m.find(SysAddonMenu, { where: { addon: body.addon }, select: { id: true } })
        if (list && list.length > 0) {
            await m.delete(SysAddonMenu, list.map(it => it.id))
        }
        const sysAddon = await m.findOne(SysAddon, { where: { name: body.addon }, select: { id: true } })
        if (sysAddon) {
            await m.delete(SysAddon, sysAddon.id)
        }
    })
    return toSuccess({})
}